package com.ourteam.education.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.education.dao.CourseDefineDAOQueryBean;
import com.ourteam.education.dao.CoursePriceDAOFactory;
import com.ourteam.education.dao.CoursePriceDAOQueryBean;
import com.ourteam.education.domain.CourseDefineBean;
import com.ourteam.education.domain.CoursePriceBean;
import com.ourteam.education.service.ICourseManageService;
import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.frame.web.OperationResult;

@Controller
@CrossOrigin
@RequestMapping("/api/EDUCATION/courseManage")
public class CourseManageAPI
		extends AbstractCRUDServiceController<CourseDefineDAOQueryBean, CourseDefineSearchBean, CourseDefineBean> {

	@Autowired
	private ICourseManageService courseManageService;

	@Override
	protected CourseDefineDAOQueryBean newQueryBean() {
		return new CourseDefineDAOQueryBean();
	}

	@Override
	protected int queryCount(CourseDefineDAOQueryBean queryBean) throws Exception {
		return courseManageService.queryCourseDefineCount(queryBean);
	}

	@Override
	protected CourseDefineBean[] queryData(CourseDefineDAOQueryBean queryBean) throws Exception {
		return courseManageService.queryCourseDefines(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {

		courseManageService.deleteCourseDefines(ids);

	}

	@Override
	protected CourseDefineBean getDataById(long id) throws Exception {
		return courseManageService.getCourseDefineById(id);
	}

	@Override
	protected void saveData(CourseDefineBean dataBean) throws Exception {

		if (dataBean.getId() == 0) {
			courseManageService.addNewCourseDefine(dataBean);
		} else {
			courseManageService.modifyCourseDefine(dataBean);
		}

	}

	@RequestMapping("/list")
	@ResponseBody
	public CourseDefineBean[] list(final @RequestParam(required = false) String courseName,
			final @RequestParam(required = false) Long campusId, final @RequestParam(required = false) String status)
			throws Exception {

		CourseDefineDAOQueryBean searchBean = new CourseDefineDAOQueryBean();

		CourseDefineDAOQueryBean.Criteria criteria = searchBean.createCriteria();

		if (StringUtils.isNoneBlank(status)) {
			criteria.andStatusEqualTo(status);
		}

		if (StringUtils.isNoneBlank(courseName)) {
			criteria.andCourseTypeNameLike(courseName);
		}

		if (campusId != null && campusId.longValue() != 0) {
			CoursePriceDAOQueryBean relateQueryBean = new CoursePriceDAOQueryBean();

			relateQueryBean.setDistinct(true);

			relateQueryBean.addCourseDefineIdSelectProperty();

			relateQueryBean.createCriteria().andCampusIdIn(new long[] { 0, campusId.longValue() });

			criteria.andIdIn(relateQueryBean, CoursePriceDAOFactory.getCoursePriceDAO());
		}

		return courseManageService.queryCourseDefines(searchBean);
	}

	@RequestMapping("/saveCoursePrice/{courseId}")
	@ResponseBody
	public OperationResult saveCoursePrice(final @PathVariable("courseId") long courseId,
			final @RequestBody CoursePriceConfigBean[] coursePrices) throws Exception {

		List<CoursePriceBean> coursePriceBeans = new ArrayList<CoursePriceBean>();

		for (int i = 0; i < coursePrices.length; i++) {

			PriceBean[] priceBeans = coursePrices[i].getCoursePrice();

			for (int j = 0; j < priceBeans.length; j++) {
				CoursePriceBean coursePriceBean = new CoursePriceBean();
				coursePriceBean.setId(priceBeans[j].getId());
				coursePriceBean.setCourseDefineId(courseId);
				coursePriceBean.setCampusId(coursePrices[i].getKey());
				coursePriceBean.setAmmount(priceBeans[j].getAmmount());
				coursePriceBean.setPrice(priceBeans[j].getPrice());
				coursePriceBean.setPriceUnit(priceBeans[j].getPriceUnit());
				coursePriceBean.setChargingMode(priceBeans[j].getChargeModel());
				coursePriceBeans.add(coursePriceBean);
			}

		}

		this.courseManageService
				.saveCoursePrice(coursePriceBeans.toArray(new CoursePriceBean[coursePriceBeans.size()]));

		return OperationResult.SUCCESS_RESULT;
	}

	@RequestMapping("/getCoursePrice")
	@ResponseBody
	public CoursePriceConfigBean[] getCoursePrice(final @RequestParam("courseId") long courseId) throws Exception {

		CoursePriceBean[] coursePriceBeans = this.courseManageService.getCoursePrice(courseId);

		Map<Long, CoursePriceConfigBean> coursePriceMap = new HashMap<>();

		for (int i = 0; i < coursePriceBeans.length; i++) {

			if (coursePriceMap.containsKey(coursePriceBeans[i].getCampusId()) == false) {
				CoursePriceConfigBean priceConfigBean = new CoursePriceConfigBean();
				priceConfigBean.setKey(coursePriceBeans[i].getCampusId());
				if (coursePriceBeans[i].getCampusId() == 0) {
					priceConfigBean.setCampus("全部校区");
				} else {
					priceConfigBean.setCampus(coursePriceBeans[i].getCampusName());
				}
				coursePriceMap.put(Long.valueOf(coursePriceBeans[i].getCampusId()), priceConfigBean);
			}

			PriceBean priceBean = new PriceBean();

			priceBean.setId(coursePriceBeans[i].getId());
			priceBean.setAmmount(coursePriceBeans[i].getAmmount());
			priceBean.setChargeModel(coursePriceBeans[i].getChargingMode());
			priceBean.setPrice(coursePriceBeans[i].getPrice());
			priceBean.setPriceUnit(coursePriceBeans[i].getPriceUnit());

			CoursePriceConfigBean configBean = coursePriceMap.get(coursePriceBeans[i].getCampusId());

			if (configBean.getCoursePrice() == null) {
				configBean.setCoursePrice(new PriceBean[] { priceBean });
			} else {
				configBean.setCoursePrice(ArrayUtils.add(configBean.getCoursePrice(), priceBean));
			}

		}

		return coursePriceMap.values().toArray(new CoursePriceConfigBean[coursePriceMap.values().size()]);
	}

}
